clear all
set more off
	
global dir = "C:\Users\mrueda\Documents\Emory\Papers\Networks_persistance\do_files\do_files_APSA21\post_JOP\Replication_BJPS\"	

cd "$dir"

use "Data\donor_level_persist_rep.dta",clear
*New donor-level family (f) and (nf) variable generation
gen nftotal_cont_num_d = total_cont_num_d if family==0
gen ftotal_cont_num_d = total_cont_num_d if family==1
gen nfcontract = contract if family==0
gen fcontract = contract if family==1
gen fruns_any = runs_any if family==1
gen fgot_above_ext = got_above_ext if family==1


keep if rank==1|rank==2
replace b5=. if b2!=0

gen treat=0
replace treat=1 if margin_victory>0&margin_victory~=.
replace treat=. if margin_victory==.

gen treat_margin_victory=treat*margin_victory



	texdoc close 
	cap erase "$dir/Tables/TableG6.tex"
	texdoc init "$dir/Tables/TableG6.tex", force

	tex \begin{table}[h]
	tex \caption{Effect of electoral victory on benefits to donors (donor-level)}\label{tab:table_benefits_ols}
	tex \centering
	tex \scalebox{.8}{
	tex \begin{tabular}{l c c c c c c} \hline
	tex Outcome:& \multicolumn{2}{c}{Total contracts}&\multicolumn{2}{c}{Receive contract}&Receive contract& Runs in 2015\\
	tex &  \multicolumn{2}{c}{(municipality)} &\multicolumn{2}{c}{(municipality)} & (outside)& \\
	tex & Non-Family &Family & Non-Family &Family  & Family &Family \\
	tex & (1) & (2) & (3) & (4)& (5) & (6)\\ \hline
	tex & & & & & &\\
	
	*preserve
	*drop contract
	*rename got_any contract
	
	*Model 1
	foreach x in  total_cont_num_d contract{
	
		*No Family
		*Regressions
		quietly: regress nf`x' treat sanc_before ilegal p_prop elec_exp pol_exp_d center rank_don_alt_all lcont_donor_102 contraloria above_lim, vce(cluster muni_code)
		quietly sum nf`x' if e(sample)
			local nfmean_`x' : di %5.3f r(mean)
			local nfsd_`x' : di %5.3f r(sd) 		
		
		regress nf`x' treat sanc_before ilegal p_prop elec_exp pol_exp_d center rank_don_alt_all lcont_donor_102 contraloria above_lim, vce(cluster muni_code)

		local nfN_`x' : di %5.0f e(N)
		local nfR2_`x' : di %5.3f e(r2)

		matrix b = e(b)
		matrix v = e(V)
		matrix res=r(table)
		
		local nfb1_`x' : di %5.3f b[1,1]
		local nfse1_`x' : di %5.3f sqrt(v[1,1])
		local nfp_v_`x' :di %5.3f res[4,1]
		local nfuci_`x': di %5.3f res[6,1]
		local nflci_`x': di %5.3f res[5,1]
		

		areg nf`x' treat sanc_before ilegal p_prop elec_exp pol_exp_d center rank_don_alt_all lcont_donor_102 contraloria above_lim if e(sample), absorb(muni_code) vce(cluster muni_code)
	
		matrix bf = e(b)
		matrix vf = e(V)
		matrix resf=r(table)
		
		local nfb1f_`x' : di %5.3f bf[1,1]
		local nfse1f_`x' : di %5.3f sqrt(vf[1,1])
		local nfp_vf_`x' :di %5.3f resf[4,1]
		local nfucif_`x': di %5.3f resf[6,1]
		local nflcif_`x': di %5.3f resf[5,1]
		
	}
use "Data\donor_level_persist_rep.dta",clear
*New donor-level family (f) and (nf) variable generation
gen nftotal_cont_num_d = total_cont_num_d if family==0
gen ftotal_cont_num_d = total_cont_num_d if family==1
gen nfcontract = contract if family==0
gen fcontract = contract if family==1
gen nfruns_any = runs_any if family==0
gen fruns_any = runs_any if family==1
gen nfgot_above_ext = got_above_ext if family==0
gen fgot_above_ext = got_above_ext if family==1

gen treat=0
replace treat=1 if margin_victory>0&margin_victory~=.
replace treat=. if margin_victory==.

gen treat_margin_victory=treat*margin_victory

	
	foreach x in  total_cont_num_d contract got_above_ext runs_any{
	
		*No Family
		*Regressions
		quietly: regress f`x' treat sanc_before ilegal p_prop elec_exp pol_exp_d center rank_don_alt_all lcont_donor_all contraloria above_lim, vce(cluster muni_code)
		quietly sum f`x' if e(sample)
			local fmean_`x' : di %5.3f r(mean)
			local fsd_`x' : di %5.3f r(sd) 		
		
		regress f`x' treat sanc_before ilegal p_prop elec_exp pol_exp_d center rank_don_alt_all lcont_donor_all contraloria above_lim, vce(cluster muni_code)

		local fN_`x' : di %5.0f e(N)
		local fR2_`x' : di %5.3f e(r2)

		matrix b = e(b)
		matrix v = e(V)
		matrix res=r(table)
		
		local fb1_`x' : di %5.3f b[1,1]
		local fse1_`x' : di %5.3f sqrt(v[1,1])
		local fp_v_`x' :di %5.3f res[4,1]
		local fuci_`x': di %5.3f res[6,1]
		local flci_`x': di %5.3f res[5,1]
		
		areg f`x' treat sanc_before ilegal p_prop elec_exp pol_exp_d center rank_don_alt_all lcont_donor_all contraloria above_lim if e(sample),absorb(muni_code) vce(cluster muni_code)
		
		matrix bf = e(b)
		matrix vf = e(V)
		matrix resf=r(table)
		
		local fb1f_`x' : di %5.3f bf[1,1]
		local fse1f_`x' : di %5.3f sqrt(vf[1,1])
		local fp_vf_`x' :di %5.3f resf[4,1]
		local fucif_`x': di %5.3f resf[6,1]
		local flcif_`x': di %5.3f resf[5,1]
		
	}
	
	

	*Continue table
	tex Electoral victory & `nfb1_total_cont_num_d' &  `fb1_total_cont_num_d'& `nfb1_contract' & `fb1_contract' & `fb1_got_above_ext' & `fb1_runs_any' \\
	tex \ \ \ \  p-value & `nfp_v_total_cont_num_d' &  `fp_v_total_cont_num_d' & `nfp_v_contract' & `fp_v_contract' & `fp_v_got_above_ext' & `fp_v_runs_any' \\
	tex \ \ \ \ CI 95\%  & [`nflci_total_cont_num_d',`nfuci_total_cont_num_d'] & [`flci_total_cont_num_d',`fuci_total_cont_num_d'] &  [`nflci_contract',`nfuci_contract'] & [`flci_contract',`fuci_contract'] & [`flci_got_above_ext',`fuci_got_above_ext'] & [`flci_runs_any',`fuci_runs_any']\\
	tex & & & \\

	tex Electoral victory (FE) & `nfb1f_total_cont_num_d' &  `fb1f_total_cont_num_d'& `nfb1f_contract' & `fb1f_contract' & `fb1f_got_above_ext' & `fb1f_runs_any' \\
	tex \ \ \ \  p-value & `nfp_vf_total_cont_num_d' &  `fp_vf_total_cont_num_d' & `nfp_vf_contract' & `fp_vf_contract' & `fp_vf_got_above_ext' & `fp_vf_runs_any' \\
	tex \ \ \ \ CI 95\%  & [`nflcif_total_cont_num_d',`nfucif_total_cont_num_d'] & [`flcif_total_cont_num_d',`fucif_total_cont_num_d'] &  [`nflcif_contract',`nfucif_contract'] & [`flcif_contract',`fucif_contract'] & [`flcif_got_above_ext',`fucif_got_above_ext'] & [`flcif_runs_any',`fucif_runs_any']\\
	tex & & & \\	
	
	
	tex Observations &`nfN_total_cont_num_d' &  `fN_total_cont_num_d' & `nfN_contract'& `fN_contract' & `fN_got_above_ext' & `fN_runs_any' \\
	tex Mean & `nfmean_total_cont_num_d' &`fmean_total_cont_num_d'  &  `nfmean_contract' & `fmean_contract' & `fmean_got_above_ext' & `fmean_runs_any' \\ \hline
	tex \end{tabular}}
	tex \parbox{160mm}{ \footnotesize{
	tex \footnotesize{Ordinary least squares (OLS) estimates of the effect of donating to the winner on benefits received by the donor. All models include as controls: candidate's illegal registration of ID, being sanctioned by the Office of the Inspector General, elected posts, ran as candidate in past elections, party is not left-wing or right-wing, and non-family donations as a fraction of campaign revenue, logged value of donation, donated above legal limit, sanctioned, rank of donation among all family and non-family donors. FE denote municipality fixed effects. Confidence intervals and p-values with clusters at the municipality level.
    tex }
	tex }
	tex }
	tex \end{table}
	cap texdoc close 

	
	